package cn.edu.ustc.brainor.ustcgate;

import android.os.AsyncTask;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

/**
 * Created by 欧伟科 on 2016/10/14.
 */

public class Mail extends AsyncTask<String, Void, String> {
    private void sendMail(String... 位置) {
        Properties props = new Properties();
        props.put("mail.smtp.host", mailServer);
        Session session = Session.getInstance(props, null);

        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(userMail);
            msg.setRecipients(Message.RecipientType.TO,
                    userMail);
            msg.setSubject("[定位]");
            msg.setSentDate(new Date());
            msg.setText("http://api.map.baidu.com/marker?location=" + 位置[0] + "," + 位置[1] + "&title=" + "待定" + "&content=误差" + 位置[2] + "米&output=html&coord_type=wgs84");
            Transport.send(msg, userMail, password);
        } catch (MessagingException mex) {
            System.out.println("send failed, exception: " + mex);
        }
    }

    private String userMail = "ouweike@sina.com";
    private String password = "oucheng6456?";
    private String mailServer = "smtp.sina.com";


    @Override
    protected String doInBackground(String... 位置) {
        sendMail(位置);
        return "success";
    }
}
